﻿Imports Trinkets.Core

Public Class LookupControl

    Private _database As CsvDatabase = Nothing

    Public ReadOnly Property Database As CsvDatabase
        Get
            Return _database
        End Get
    End Property

    Private _table As TableSchema = Nothing

    Public ReadOnly Property Table As TableSchema
        Get
            Return _table
        End Get
    End Property

    Private _selectedRecord As BaseEntity = Nothing

    Public Property SelectedRecord As BaseEntity
        Get
            Return _selectedRecord
        End Get
        Set(ByVal value As BaseEntity)
            _selectedRecord = value
            UpdateTextBox()
        End Set
    End Property

    Public Sub New(ByVal database As CsvDatabase, ByVal table As TableSchema)
        InitializeComponent()
        _database = database
        _table = table
    End Sub

    Private Sub SelectButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles SelectButton.Click
        SelectedRecord = LookupWindow.RequestRecord(_database, _table, _selectedRecord)
    End Sub

    Private Sub Rectangle_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Rectangle.MouseLeftButtonUp

        If _selectedRecord IsNot Nothing Then

            ' Generate and show the edit form
            Dim Form As EditWindow = EditWindow.Generate(_database, _table, _selectedRecord)
            If Form.ShowDialog = True Then UpdateTextBox()

        End If

    End Sub

    Private Sub UpdateTextBox()

        TextBox.Text = String.Empty
        If _selectedRecord IsNot Nothing Then TextBox.Text = _selectedRecord.ToString()

    End Sub

End Class
